package com.cn.ant.modules.score.web;

import com.cn.ant.common.persistence.Page;
import com.cn.ant.common.utils.AjaxJson;
import com.cn.ant.common.utils.StringUtils;
import com.cn.ant.common.web.BaseController;
import com.cn.ant.modules.score.entity.SchoolLqRate;
import com.cn.ant.modules.score.service.SchoolLqRateService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 学校录取概率Controller
 * @author 黄根华
 * @version 2015-10-11
 */
@Controller
@RequestMapping(value = "${adminPath}/score/schoolLqRate")
public class SchoolLqRateController extends BaseController {

	@Autowired
	private SchoolLqRateService schoolLqRateService;
	
	@ModelAttribute
	public SchoolLqRate get(@RequestParam(required=false) String id) {
		SchoolLqRate entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = schoolLqRateService.get(id);
		}
		if (entity == null){
			entity = new SchoolLqRate();
		}
		return entity;
	}
	
	@RequiresPermissions("score:schoolLqRate:view")
	@RequestMapping(value = {"list", ""})
	public String list(SchoolLqRate schoolLqRate, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SchoolLqRate> page = schoolLqRateService.findPage(new Page<SchoolLqRate>(request, response), schoolLqRate); 
		model.addAttribute("page", page);
		return "modules/score/schoolLqRateList";
	}

	@RequiresPermissions("score:schoolLqRate:view")
	@RequestMapping(value = "form")
	public String form(SchoolLqRate schoolLqRate, Model model) {
		model.addAttribute("schoolLqRate", schoolLqRate);
		return "modules/score/schoolLqRateForm";
	}

	@RequiresPermissions("score:schoolLqRate:edit")
	@RequestMapping(value = "save")
	@ResponseBody
	public AjaxJson save(SchoolLqRate schoolLqRate, HttpServletRequest request,Model model) {
		AjaxJson jo = new AjaxJson();
		try {
			int year = Integer.parseInt(request.getParameter("year"));
			String province = "43";
			schoolLqRateService.initLqRate(year, province, "湖南");
			msg = "初始化学校录取概率成功";
			jo.setMsg(msg);
			jo.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
            jo.setSuccess(false);
            jo.setMsg("操作失败,请联系管理员");
		}
		return jo;
	}
	
	@RequiresPermissions("score:schoolLqRate:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public AjaxJson delete(SchoolLqRate schoolLqRate, RedirectAttributes redirectAttributes) {
		AjaxJson jo = new AjaxJson();
        if (StringUtils.isNotBlank(schoolLqRate.getId())) {
            schoolLqRateService.delete(schoolLqRate);
            jo.setMsg("删除学校录取概率成功");
        } else {
            jo.setSuccess(false);
            jo.setMsg("参数错误,删除失败");
        }
        return jo;
	}

}